/****************************************************
 * @class OwnerSelectionWindow
 * @extends Ext.Window
 ***************************************************/
OwnerSelectionWindow = Ext.extend(Ext.Window,{
	ownerSelectionPanel: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.ownerSelectionPanel = new OwnerSelectionPanel({id: "pnl_owner_selection"});
		
		OwnerSelectionWindow.superclass.constructor.call(this, {
			layout: 'form',
			width: 640,
			height: 480,
			closeAction: 'hide',
			plain: true,
			buttonAlign: 'center',
			modal: true,
			resizable: false,
			layout: 'fit',
			items: [this.ownerSelectionPanel],
			buttons: [{
			    text: "确定",
			    scope: this,
			    disabled: false,
			    handler: function(){
			    	var owners = document.getElementById("_collectOwners").value;
			    	
			    	var saveMask = new Ext.LoadMask(Ext.getBody(), {
						msg: "信息保存中，请稍后...",
						removeMask: true
					});
					saveMask.show();

			    	Ext.Ajax.request({
						url: '/pdm/category/assignProjectOwners.htm',
						method: 'post',
						params: {
							userList: owners,
							projectId: Ext.fly("_selected_pro_id").dom.value
						},
						success: function(response,options){										
							//cur_node.parentNode.reload(true,true);
							//var win_owner_selection = Ext.getCmp("win_owner_selection");
							
							/*
							var result = Ext.decode(response.responseText).message;
							Ext.Msg.alert("信息:", result);
							*/
							saveMask.hide();
							
							var my_project_tree_grid = Ext.getCmp("my_project_tree_grid");
			    			var cur_node = my_project_tree_grid.getSelectionModel().getSelectedNode();
							
							cur_node.parentNode.reload(true,true);
							
							//win_owner_selection.ownerSelectionPanel.reset();
			    			//win_owner_selection.hide();
						},
						failure: function(response){
						
							var win_owner_selection = Ext.getCmp("win_owner_selection");
							win_owner_selection.ownerSelectionPanel.reset();
			    			win_owner_selection.hide();
			    			
			    			saveMask.hide();
			    			
							var result = Ext.decode(response.responseText).message;
							Ext.Msg.alert("信息:", result);
						}
					});
			    	
			    	this.ownerSelectionPanel.reset();
			    	this.hide();
			    	
			    	Ext.fly("_collectOwners").dom.value = "";
			    }
			},{
			    text: "关闭",
			    disabled: false,
			    scope: this,
			    handler: function(){
			    	this.hide();
			    }
			}]
		});
		
	},
	reset: function(){
		this.ownerSelectionPanel.reset();
	}
});

/****************************************************
 * @class OwnerSelectionPanel
 * @extends Ext.FormPanel
 ***************************************************/
OwnerSelectionPanel = Ext.extend(Ext.FormPanel,{
	tree: null,
	fuzzySearch: null,
	ownerSelect: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.tree = new UserTree({id: "tree_user"});
		this.ownerSelect = new OwnerSelect({id: "gp_owner_selected"});
		this.fuzzySearch = new UserFuzzySearchPanel({id: "gp_user_fuzzySearch"});
		
		OwnerSelectionPanel.superclass.constructor.call(this, {
			region: 'center',
			border: false,
			width: 500,
			height: 400,
			layout: 'column',
			viewConfig: {forceFit: true},
			buttonAlign: 'center',
			items: [{
				columnWidth: 0.59,
				layout: 'fit',
				border: false,
				height: 410,
				items:[{
		            xtype: 'tabpanel',
		            enableTabScroll: true,
					activeTab: 0,
					items:[{
						title: "Org",
						id: 'tab_org_owner',
						iconCls: 'cls_org_tab',
						border: false,
						layout: 'fit',
						height: 400,
						items: [this.tree]
					},{
						title: "Search",
						id: 'tab_search_owner',
						iconCls: 'cls_search_tab',
						layout: 'fit',
						border: false,
						height: 400,
						items: [this.fuzzySearch]
					}]
		        }]
			},{
				columnWidth: 0.01,
				border: false,
				height: 410,
				layout: 'fit'
			},{
				columnWidth: 0.40,
				height: 410,
				layout: 'fit',
				items:[this.ownerSelect]
			}]
		});
	},
	reset: function(){
		this.ownerSelect.reload();
	}
});

/****************************************************
 * @class UserTree
 * @extends Ext.tree.TreePanel
 ***************************************************/
UserTree = Ext.extend(Ext.tree.TreePanel,{
	_collectReCosign: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		UserTree.superclass.constructor.call(this, {
			autoScroll: true,
			border: false,
			width: 200,
			height: 300,
			rootVisible: false,
			singleExpand: true,
			containerScroll: true,
			loader: new Ext.tree.TreeLoader({dataUrl: '/pdm/hr/org.htm'}),
			root: new Ext.tree.AsyncTreeNode({	
			   id: '10000000',
			   text: "IVO 龙腾光电",
			   draggable: false
			})
		});
		
		this.on('dblclick', function(node){
			if(node.leaf==true)
			{
				var collectReCosign = Ext.fly("_collectOwners").dom.value;
				
				if(collectReCosign.indexOf(node.id) == -1)
				{
					if(collectReCosign == "" ) collectReCosign = node.id;
					else collectReCosign = collectReCosign+","+node.id;
					
					Ext.fly("_collectOwners").dom.value = collectReCosign;
					
					var selectReCosign = Ext.getCmp("gp_owner_selected");
					
					selectReCosign.setReCosigners(collectReCosign);
					selectReCosign.load();
				}else{
					Ext.Msg.alert("提示","人员["+ node.text +"] 已选择");
				}
			}
			else Ext.Msg.alert("温馨提示：", "请您选择人员！");
		});
	}
});

/****************************************************
 * @class OwnerSelect
 * @extends Ext.grid.GridPanel
 ***************************************************/
OwnerSelect = Ext.extend(Ext.grid.GridPanel,{
	collectReCosign: null,
	condition: null,
	cm: null,
	store: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.cm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
			{header:"员工工号", dataIndex:'employee_ID',width:100,sortable:true},
			{header:"员工名称", dataIndex:'employeeName',width:150}
		]);
		
		this.condition = {};
		
		this.store = new Ext.data.Store({
		    baseParams: this.condition, 
		    proxy: new Ext.data.HttpProxy({
		    	method: 'POST',  
				url: '/pdm/hr/getReCosignerList.htm'
			}),
		    reader: new Ext.data.JsonReader({
				totalProperty: 'totalProperty',
				root: 'root',
				id: 'employee_ID'
			},[
				{name: 'employee_ID'},
		        {name: 'employeeName'}
			])
		});
				
		OwnerSelect.superclass.constructor.call(this, {
			region: 'center',
			border: false,
			loadMask: true,
			width: 300,
			height: 500,
			cls: 'x-rb',
			viewConfig: {forceFit: true},
			ds: this.store,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
			cm: this.cm,
			tbar: ['->',{
				text: "清空",
				iconCls: 'cls_clean_all',
				scope: this,
				handler: function(){
					Ext.fly("_collectOwners").dom.value = "";
					this.setReCosigners("");
					this.load();
				}
			}]
		});
		
		this.on('dblclick', function(grid, rowIndex, e){
			var row = this.getSelectionModel().getSelected();
			var collectReCosign = document.getElementById("_collectOwners").value;
			
			var collectReCosigns = collectReCosign.split(",");
			var j = collectReCosigns.length;
			for(var i=0;i < j;i++)
			{
				if(collectReCosigns[i] == row.get("employee_ID"))
				{
					for(var t=i;t<j;t++)
					{
						collectReCosigns[t]=collectReCosigns[t+1];
					}
					j=j-1;
				}
			}
			if(j==0) collectReCosign = "";
			else
			{
				for(var m=0;m<j;m++)
				{
					if(m==0) collectReCosign=collectReCosigns[m];
					else collectReCosign = collectReCosign+","+collectReCosigns[m];
				}
			}
			document.getElementById("_collectOwners").value = collectReCosign;
			this.condition.reCosignerList = collectReCosign;
			this.store.load();
		}); 
	},
	load: function() {
		this.store.load();
	},
	setReCosigners: function(c) {
		this.condition.reCosignerList = c;
	},
	reload: function(){
		this.condition.reCosignerList = "";
		this.store.load();
	}
});

/****************************************************
 * @class UserFuzzySearchPanel
 * @extends Ext.grid.GridPanel
 ***************************************************/
UserFuzzySearchPanel = Ext.extend(Ext.grid.GridPanel,{
	condition: null,
	cm: null,
	store: null,
	searchField: null,
	pagingToolbar: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		this.cm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
			{header:"姓名", dataIndex:'employeeName',width:120},
			{header:"英文名", dataIndex:'employeeName_EN',width:150},
			{header:"部门", dataIndex:'deptName',width:150}
		]);
		
		this.condition = {};
		this.condition.limit = 50;
		
		this.store = new Ext.data.Store({
		    baseParams: this.condition, 
		    proxy: new Ext.data.HttpProxy({
		    	method: 'POST',  
				url: '/pdm/hr/getEmployeeList.htm'
			}),
		    reader: new Ext.data.JsonReader({
				totalProperty: 'totalProperty',
				root: 'root',
				id: 'employee_ID'
			},[
				{name: 'employee_ID'},
		        {name: 'employeeName'},
		        {name: 'employeeName_EN'},
		        {name: 'deptName'}
			])
		});
		//this.store.load();
		this.searchField = new Ext.ux.form.SearchField({store: this.store,width:200});
		
		this.pagingToolbar = new Ext.PagingToolbar({
			pageSize: 50,
			store: this.store,
			displayInfo: true,
			displayMsg: "第{0}~{1}条,共{2}",
			emptyMsg: "没有记录",
			refreshText: "刷新",
			beforePageText: "第",
			afterPageText: "页,共{0}页",
			firstText: "第一页",
			prevText: "上一页",
			nextText: "下一页",
			lastText: "最后一页"
		})
		
		UserFuzzySearchPanel.superclass.constructor.call(this, {
			region: 'center',
			border: false,
			loadMask: true,
			width: 300,
			height: 500,
			cls: 'x-rb',
			viewConfig: {forceFit: true},
			ds: this.store,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
			cm: this.cm,
			tbar: [this.searchField],
			bbar: this.pagingToolbar
		});
		
		this.getSelectionModel().on('rowselect', function(sm, rowIdx, r){
			document.getElementById("_select_owner").value = r.get("employee_ID");
		});
		
		this.on('dblclick', function(rowIdx, r){
			var row = this.getSelectionModel().getSelected();
			var collectReCosign = Ext.fly("_collectOwners").dom.value;
			
			if(collectReCosign.indexOf(row.get("employee_ID")) == -1)
			{
				if(collectReCosign == "") collectReCosign = row.get("employee_ID");
				else collectReCosign = collectReCosign + "," + row.get("employee_ID");
				
				Ext.fly("_collectOwners").dom.value = collectReCosign;
				
				var selectReCosign = Ext.getCmp("gp_owner_selected");
				selectReCosign.setReCosigners(collectReCosign);
				selectReCosign.load();
			}else{
				Ext.Msg.alert("提示","人员["+row.get("employeeName")+"] 已选择");
			}
		});
		
	}
});